package com.google.code.greenplate;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ShareRideRequest extends Activity {
	Button btnXML;
	Button btnJSON;
	TextView tvData;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.share_ride_request);
                
        tvData = (TextView) findViewById(R.id.txtData);
        btnXML = (Button) findViewById(R.id.btnXML);
        btnXML.setOnClickListener(new Button.OnClickListener()
        {
        	public void onClick(View v)
        	{ 
        		
        		examineXMLFile2();

        		/*
        		try {
					examineXMLFile3();
				} catch (XmlPullParserException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				*/

        	}
        });
        
        btnJSON = (Button) findViewById(R.id.btnJSON);
        btnJSON.setOnClickListener(new Button.OnClickListener()
        {
        	public void onClick(View v)
        	{
        		examineJSONFile();
        	}
        });
        
    }
    
    void examineXMLFile3() throws XmlPullParserException, IOException
    {
    	StringBuilder sb = new StringBuilder();

    	InputSource is = new InputSource(getResources().openRawResource(R.raw.transport));

    	XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    	factory.setNamespaceAware(true);
    	XmlPullParser xpp = factory.newPullParser();

    	xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
    	InputStream iss=is.getByteStream();
    	xpp.setInput(iss,"UTF-8");
    	int eventType = xpp.getEventType();
    	while (eventType != XmlPullParser.END_DOCUMENT) {
    		if(eventType == XmlPullParser.START_DOCUMENT) {
    			sb.append("Start document\n");
    		} else if(eventType == XmlPullParser.END_DOCUMENT) {
    			sb.append("End document\n");
    		} else if(eventType == XmlPullParser.START_TAG) {
    			sb.append("Start tag: "+xpp.getName() + "\n");
    		} else if(eventType == XmlPullParser.END_TAG) {
    			sb.append("End tag: "+xpp.getName()+ "\n");
    		} else if(eventType == XmlPullParser.TEXT) {
    			sb.append("Text: "+xpp.getText()+ "\n");
    		}
    		eventType = xpp.next();
    	}

    	tvData.setText(sb);


    }   
    
    void examineXMLFile2()
    {
    	URL url;
    	try {
    		
    		String urlString = "http://192.168.1.100:8080/services/transport";
    		
    		url = new URL(urlString);
    		
    		URLConnection connection = url.openConnection();
    		   		
    		HttpURLConnection httpConnection = (HttpURLConnection)connection;
    		int responseCode = httpConnection.getResponseCode();
    		
    		if (responseCode == HttpURLConnection.HTTP_OK) {
    			InputStream is = httpConnection.getInputStream();

    			//InputSource is = new InputSource(getResources().openRawResource(R.raw.xmltwitter));
    			//InputSource is = new InputSource(getResources().openRawResource(R.raw.transport));

    			// create the factory
    			SAXParserFactory factory = SAXParserFactory.newInstance();

    			// create a parser
    			SAXParser parser = factory.newSAXParser();

    			// create the reader (scanner)
    			XMLReader xmlreader = parser.getXMLReader();

    			// instantiate our handler
    			RideOfferFeedHandler tfh = new RideOfferFeedHandler();

    			// assign our handler
    			xmlreader.setContentHandler(tfh);

    			// perform the synchronous parse
    		
    			InputSource iss = new InputSource(is);
    			xmlreader.parse(iss);

    			// should be done... let's display our results
    			tvData.setText(tfh.getResults());
    		}
    	}
    	catch (Exception e) {
    		tvData.setText("Exception: " + e.getMessage());
    	}

    }
    
    void examineXMLFile()
    {
    	try {
 
    		//InputSource is = new InputSource(getResources().openRawResource(R.raw.xmltwitter));
    		InputSource is = new InputSource(getResources().openRawResource(R.raw.transport));
    			
	        // create the factory
	        SAXParserFactory factory = SAXParserFactory.newInstance();
	
	        // create a parser
	        SAXParser parser = factory.newSAXParser();

	        // create the reader (scanner)
	        XMLReader xmlreader = parser.getXMLReader();

	        // instantiate our handler
	        RideOfferFeedHandler tfh = new RideOfferFeedHandler();

	        // assign our handler
	        xmlreader.setContentHandler(tfh);

	        // perform the synchronous parse
	        xmlreader.parse(is);
	        
	        // should be done... let's display our results
	        tvData.setText(tfh.getResults());
    	}
    	catch (Exception e) {
    		tvData.setText(e.getMessage());
    	}

    }
    
    void examineJSONFile()
    {
    	try
    	{
    		String x = "";
    		InputStream is = this.getResources().openRawResource(R.raw.jsontwitter);
    		byte [] buffer = new byte[is.available()];
    		while (is.read(buffer) != -1);
    		String jsontext = new String(buffer);
    		JSONArray entries = new JSONArray(jsontext);
    		
    		x = "JSON parsed.\nThere are [" + entries.length() + "]\n\n";
    		
    		int i;
    		for (i=0;i<entries.length();i++)
    		{
    			JSONObject post = entries.getJSONObject(i);
    			x += "------------\n";
    			x += "Date:" + post.getString("created_at") + "\n";
    			x += "Post:" + post.getString("text") + "\n\n";
    		}
    		tvData.setText(x);
    	}
    	catch (Exception je)
    	{
    		tvData.setText("Error w/file: " + je.getMessage());
    	}
    }
    
}